package com.kaaction.modules.crab.api;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kaaction.common.utils.CacheUtils;
import com.kaaction.common.web.BaseController;

@Controller
@RequestMapping(value = "/api/v1/crabValidateCodeApi")
public class CrabValidateCode extends BaseController{
	
	/**
	 * 获取验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/getValidateCode")
	public void getValidateCode(HttpServletRequest request,HttpServletResponse response) throws IOException{
	/*	//创建空白图片
		BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
		//获取图片画笔
		Graphics g =image.getGraphics();
		Random r = new Random();
		//设置画笔颜色
		g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
		//绘制背景颜色
		g.fillRect(0, 0, 100, 28);
		
		//调用自定义的方法，获取长度为6的字母数字组合的字符串
*/		String number = getNumber(4);
		logger.info("number="+number);
		CacheUtils.put("kaaCode"+request.getRemoteAddr(), number);
		/*g.setColor(new Color(0,0,0));
		g.setFont(new Font(null,Font.BOLD,20));
		//设置颜色字体后，绘制字符串
		g.drawString(number, 5, 25);
		
		response.setContentType("image/jpeg");
		OutputStream ops = response.getOutputStream();
		ImageIO.write(image, "jpeg", ops);
		ops.close();*/
		super.writeAjaxResponse(response,number);
	}

	private String getNumber(int size) {
		String str ="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String number="";
		Random r = new Random();
		for(int i=0;i<size;i++){
			number+=str.charAt(r.nextInt(str.length()));
		}
		return number;
	}

}
